package sdk.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.org.apache.bcel.internal.generic.NEW;

import dao.impl.ContractDAOImpl;
import dao.impl.DeptDAOImpl;
import dao.impl.DutyDAOImpl;
import dao.impl.EducationDAOImpl;
import dao.impl.EmpDAOImpl;

import sdk.service.ContractManager;
import util.Page;

import model.Contract;
import model.Emp;

public class GetContractServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int pageNo = 1;
		int pageSize = 2;
		Page<Contract> page = null;
		String keycode = "";
		
		System.out.println(keycode);
		
		
		
		if (null != request.getParameter("searchtype")) {
			request.getSession().setAttribute("searchtype",
					request.getParameter("searchtype"));
		}
		if (null != request.getParameter("keycode")) {
			request.getSession().setAttribute("keycode",
					request.getParameter("keycode"));
			keycode = request.getParameter("keycode");
		} else {
			keycode = (String) request.getSession().getAttribute("keycode");
		}

		try {
			if (request.getParameter("pageNo") != null) {
				pageNo = Integer.parseInt(request.getParameter("pageNo"));
			}
			if (request.getParameter("pageSize") != null) {
				pageSize = Integer.parseInt(request.getParameter("pageSize"));
				request.getSession().setAttribute("pageSize", pageSize);
			}
			if (request.getSession().getAttribute("pageSize") != null) {
				pageSize = (Integer) request.getSession().getAttribute(
						"pageSize");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if (keycode != "") {
			if ("empno".equals(request.getSession().getAttribute("searchtype"))) {
				
				
				System.out.println("empno");
				
				page = new ContractManager().getByType(pageNo, pageSize,
						new EmpDAOImpl().getEmpById(new EmpDAOImpl().getIDByEmpno(keycode)));
			} else if ("dutyn".equals(request.getSession().getAttribute(
					"searchtype"))) {


				System.out.println("dutyn");
				
				page = new ContractManager().getByType(pageNo, pageSize,
						new DutyDAOImpl().getDutyByName(keycode));
			} else if ("conno".equals(request.getSession().getAttribute(
					"searchtype"))) {


				System.out.println("conno");
				
				page = new ContractManager().getByType(pageNo, pageSize,
						new ContractDAOImpl().getById(keycode));
			} else {
				page = new ContractManager().getAllContract(pageNo, pageSize);
				System.out.println("getall......1");
			}

		} else {
			
			System.out.println("getall......2");
			
			page = new ContractManager().getAllContract(pageNo, pageSize);
		}
		// 计算起始记录跟结束记录
		request.setAttribute("page", page);
		request.getRequestDispatcher("/Dossier/Contract.jsp").forward(request, response);
	}

}
